package io.renren.modules.sys.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.utils.Query;
import io.renren.modules.sys.entity.DictDataManagerEntity;
import io.renren.modules.sys.service.DictDataManagerService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.sys.entity.DictManagerEntity;
import io.renren.modules.sys.service.DictManagerService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 *
 *
 * @author hlw
 * @email sunlightcs@gmail.com
 * @date 2023-12-08 10:07:17
 */
@RestController
@RequestMapping("sys/dictmanager")
public class DictManagerController {
    @Autowired
    private DictManagerService dictManagerService;

    @Autowired
    private DictDataManagerService dictDataManagerService;

    /**
     * 列表
     */
 /*   @RequestMapping("/list")
    @RequiresPermissions("sys:dictmanager:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = dictManagerService.queryPage(params);

        return R.ok().put("page", page);
    }
*/

    /**
     * 列表
     */
    @GetMapping("/list")
    @RequiresPermissions("sys:dictmanager:list")
    public List<DictManagerEntity> list(){
        List<DictManagerEntity> menuList = dictManagerService.list();
        return menuList;
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:dictmanager:info")
    public R info(@PathVariable("id") Long id){
		DictManagerEntity dictManager = dictManagerService.getById(id);

        return R.ok().put("dictManager", dictManager);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:dictmanager:save")
    public R save(@RequestBody DictManagerEntity dictManager){
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("dict_code",dictManager.getDictCode());
            DictManagerEntity f = this.dictManagerService.getOne(queryWrapper) ;
            if (ObjectUtil.isNotNull(f)){
               return R.error("字典编码已存在，请重新输入！");
            }else {
               dictManagerService.save(dictManager);
            }

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:dictmanager:update")
    public R update(@RequestBody DictManagerEntity dictManager){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("dict_code",dictManager.getDictCode());
        queryWrapper.ne("id",dictManager.getId());
        DictManagerEntity f = this.dictManagerService.getOne(queryWrapper) ;
        if (ObjectUtil.isNotNull(f)){
            return R.error("字典编码已存在，请重新输入！");
        }else {
            dictManagerService.updateById(dictManager);
        }
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:dictmanager:delete")
    public R delete(@RequestBody Long[] ids){
        List<DictManagerEntity> list = this.dictManagerService.listByIds(Arrays.asList(ids));
        list.forEach(i->{
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("dict_code",i.getDictCode());
            dictDataManagerService.remove(queryWrapper);
        });

		dictManagerService.removeByIds(Arrays.asList(ids));


        return R.ok();
    }

}
